/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.dao.impl;

import com.ivanbiz.dao.ApproveItemDAO;
import com.ivanbiz.dao.KasAwalDAO;
import com.ivanbiz.helper.GlobalConstant;
import com.ivanbiz.helper.HibernateUtil;
import com.ivanbiz.model.ApproveItem;
import com.ivanbiz.model.Item;
import com.ivanbiz.model.User;
import java.util.Date;
import java.util.List;
import javax.swing.JTable;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrator
 */
public class ApproveItemDAOImpl extends GenericDAOImpl implements ApproveItemDAO {

    @Override
    public void updateOrSave(List<ApproveItem> listApproveItem, JTable tableData, User user) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            KasAwalDAO kasAwalDAO = new KasAwalDAOImpl();
            String noPO = "";
            double amount = 0;
            for (int x = 0; x < listApproveItem.size(); x++) {
                ApproveItem approveItem = listApproveItem.get(x);
                //System.out.println("" + approveItem.getHargaModal());
                if ((Boolean) tableData.getValueAt(x, 0)) {
                    approveItem.setDateApprove(new Date());
                    approveItem.setUserApprove(user.getUserName());
                    approveItem.setStatus(GlobalConstant.Y);
                    Criteria crit = session.createCriteria(Item.class);
                    crit.add(Restrictions.eq("kodeBarcode", approveItem.getKodeBarcode()));
                    Item item = (Item) crit.uniqueResult();
                    noPO = approveItem.getNoPO();
                    amount += approveItem.getHargaModal();
                    // System.out.println(amount);
                    if (null == item) {
                        Item items = new Item();
                        items.setKodeBarcode(approveItem.getKodeBarcode());
                        items.setNamaItem(approveItem.getNamaItem());
                        items.setDescription(GlobalConstant.EMPTY_STRING);
                        items.setHargaModal(approveItem.getHargaModal());
                        items.setStock(approveItem.getQuantity());
                        items.setProfit(0);
                        items.setPpn(0);
                        items.setHargaJual(0);
                        items.setHargaJual2(0);
                        items.setLockSet(0);
                        session.save(items);
                    } else {
                        item.setStock(item.getStock() + approveItem.getQuantity());
                        item.setHargaModal(approveItem.getHargaModal());
                        session.update(item);
                    }
                    session.update(approveItem);
                    kasAwalDAO.saveKasAwalAndHistory(session, noPO, amount * approveItem.getQuantity());
                }


            }
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }
}
